package com.love_weinuo.application;

import android.app.Application;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.support.multidex.MultiDex;
import android.text.TextUtils;
import android.util.Log;

import com.love_weinuo.bean.Constants;
import com.love_weinuo.greendao.gen.DaoMaster;
import com.love_weinuo.greendao.gen.DaoSession;
import com.love_weinuo.ui.activity.TalkActivity;
import com.love_weinuo.util.LogUtil;
import com.lzy.okgo.OkGo;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.imsdk.session.SessionWrapper;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.qcloud.uikit.BaseUIKitConfigs;
import com.tencent.qcloud.uikit.IMEventListener;
import com.tencent.qcloud.uikit.TUIKit;
import com.tencent.qcloud.uikit.common.utils.UIUtils;
import java.util.HashSet;
import java.util.Set;
import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;

import static com.love_weinuo.bean.Constants.WX_PAY_APPID;

/**
 * Created by valexhuang on 2018/6/21.
 */

public class LoveApplication extends Application {

    public static boolean isDebug = true;//TODO 上线关闭
    public static LoveApplication instance;
    public static String registrationId;
    public static IWXAPI api;
    private com.love_weinuo.greendao.gen.DaoMaster.DevOpenHelper mHelper;
    private SQLiteDatabase db;
    private com.love_weinuo.greendao.gen.DaoMaster mDaoMaster;
    int TAG = 0x1314;
    private com.love_weinuo.greendao.gen.DaoSession mDaoSession;

    public static LoveApplication getInstance() {
        if (instance == null) {
            instance = new LoveApplication();
        }
        return instance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        //判断是否是在主线程
        if (SessionWrapper.isMainProcess(getApplicationContext())) {
            TUIKit.init(LoveApplication.getInstance().getApplicationContext(), Constants.TENCENT_IM_APP_ID, BaseUIKitConfigs.getDefaultConfigs());
            customConfig();
        }

        //       if (LeakCanary.isInAnalyzerProcess(this)) {
        //            return;
        //        }
        //        LeakCanary.install(this);

        //true 会打印日志
        LogUtil.init(isDebug);

        //初始化okgo
        OkGo.init(this);

        //初始化bugly  80a3c426-ace3-4eb6-a42e-34b037534b0c
        CrashReport.initCrashReport(getApplicationContext(), "b17249d0db", true);

        //极光推送
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

        //设置别名
        //        setTagAndAlias("123");
        //获取设备ID
        registrationId = JPushInterface.getRegistrationID(this);

        setDatabase();
    }

    private void setDatabase() {
        mHelper = new DaoMaster.DevOpenHelper(this, "notes_db", null);
        db = mHelper.getWritableDatabase();
        mDaoMaster = new DaoMaster(db);
        mDaoSession = mDaoMaster.newSession();
    }

    public DaoSession getDaoSession() {
        return mDaoSession;
    }

    public SQLiteDatabase getDb() {
        return db;
    }

    private void customConfig() {
        if (TUIKit.getBaseConfigs() != null) {
            TUIKit.getBaseConfigs().setIMEventListener(new IMEventListener() {
                @Override
                public void onForceOffline() {
                    UIUtils.toastLongMessage("您的账号已在其它终端登录");
                    TalkActivity.login(false);
                }
            });
            //TUIKit.getBaseConfigs().setFaceConfigs(initCustomConfig());
        }
    }

    public static LoveApplication instance() {
        return instance;
    }

    public void setTagAndAlias(String uid) {
        Set<String> tags = new HashSet<>();
        if (!TextUtils.isEmpty(uid)) {
            tags.add(uid);
        }
        JPushInterface.setAliasAndTags(this, uid, tags,
                mAliasCallback);
    }

    public final TagAliasCallback mAliasCallback = new TagAliasCallback() {
        @Override
        public void gotResult(int code, String alias, Set<String> tags) {
            String logs;
            switch (code) {
                case 0:
                    logs = "Set tag and alias success极光推送别名设置成功";
                    Log.e("TAG", logs);
                    break;
                case 6002:
                    logs = "Failed to set alias and tags due to timeout. Try again after 60s.极光推送别名设置失败，60秒后重试";
                    Log.e("TAG", logs);
                    break;
                default:
                    logs = "极光推送设置失败，Failed with errorCode = " + code;
                    Log.e("TAG", logs);
                    break;
            }
        }
    };

    public String getRegistrationId(){
        return registrationId;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    public IWXAPI registerWeChat(Context context) {
        api = WXAPIFactory.createWXAPI(context, WX_PAY_APPID, true);
        api.registerApp(WX_PAY_APPID);
        return api;
    }

    //    private ArrayList<CustomFaceGroupConfigs> initCustomConfig() {
    //        ArrayList<CustomFaceGroupConfigs> groupFaces = new ArrayList<>();
    //        //创建一个表情组对象
    //        CustomFaceGroupConfigs faceConfigs = new CustomFaceGroupConfigs();
    //        //设置表情组每页可显示的表情列数
    //        faceConfigs.setPageColumnCount(5);
    //        //设置表情组每页可显示的表情行数
    //        faceConfigs.setPageRowCount(2);
    //        //设置表情组号
    //        faceConfigs.setFaceGroupId(1);
    //        //设置表情组的主ICON
    //        faceConfigs.setFaceIconPath("4349/xx07@2x.png");
    //        //设置表情组的名称
    //        faceConfigs.setFaceIconName("4350");
    //        for (int i = 1; i <= 15; i++) {
    //            //创建一个表情对象
    //            FaceConfig faceConfig = new FaceConfig();
    //            String index = "" + i;
    //            if (i < 10)
    //                index = "0" + i;
    //            //设置表情所在Asset目录下的路径
    //            faceConfig.setAssetPath("4349/xx" + index + "@2x.png");
    //            //设置表情所名称
    //            faceConfig.setFaceName("xx" + index + "@2x");
    //            //设置表情宽度
    //            faceConfig.setFaceWidth(240);
    //            //设置表情高度
    //            faceConfig.setFaceHeight(240);
    //            faceConfigs.addFaceConfig(faceConfig);
    //        }
    //        groupFaces.add(faceConfigs);
    //        faceConfigs = new CustomFaceGroupConfigs();
    //        faceConfigs.setPageColumnCount(5);
    //        faceConfigs.setPageRowCount(2);
    //        faceConfigs.setFaceGroupId(1);
    //        faceConfigs.setFaceIconPath("4350/tt01@2x.png");
    //        faceConfigs.setFaceIconName("4350");
    //        for (int i = 1; i <= 16; i++) {
    //            FaceConfig faceConfig = new FaceConfig();
    //            String index = "" + i;
    //            if (i < 10)
    //                index = "0" + i;
    //            faceConfig.setAssetPath("4350/tt" + index + "@2x.png");
    //            faceConfig.setFaceName("tt" + index + "@2x");
    //            faceConfig.setFaceWidth(240);
    //            faceConfig.setFaceHeight(240);
    //            faceConfigs.addFaceConfig(faceConfig);
    //        }
    //        groupFaces.add(faceConfigs);
    //        return groupFaces;
    //    }

}
